﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormSelectProject : FormTemplet
    {
        private int _nCustomerID = -1;

        private static ProjectData s_projectInfo = null;

        public FormSelectProject()
        {
            this.InitializeComponent();
            FormSelectProject.s_projectInfo = null;
        }

        private FormSelectProject(int nCustomerID) : this()
        {
            this._nCustomerID = nCustomerID;
        }

        private void FormSelectProject_Load(object sender, EventArgs e)
        {
            this.cbCondition.SelectedIndex = 0;
            string strQuery;
            if (this._nCustomerID > -1)
            {
                strQuery = "SELECT TOP 5 p.PKID AS '序号',c.CompanyName AS '公司',p.ProjectName AS '计划名称',p.Linkman AS '联系人',p.ProjectAddress AS '计划地址' FROM Project AS p INNER JOIN Customer AS c ON p.CustomerID=c.PKID WHERE c.PKID=" + this._nCustomerID + " ORDER BY p.PKID DESC";
            }
            else
            {
                strQuery = "SELECT TOP 5 p.PKID AS '序号',c.CompanyName AS '公司',p.ProjectName AS '计划名称',p.Linkman AS '联系人',p.ProjectAddress AS '计划地址' FROM Project AS p INNER JOIN Customer AS c ON p.CustomerID=c.PKID ORDER BY p.PKID DESC";
            }
            Projects projects = new Projects();
            DataTable dataTable = null;
            try
            {
                dataTable = projects.GetProjects(strQuery);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n出现未知错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            projects.Dispose();
            if (dataTable != null)
            {
                this.dgProject.DataSource = null;
                this.dgProject.DataSource = dataTable;
                this.dgProject.AutoResizeColumns();
                dataTable.Dispose();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string text;
            if (this._nCustomerID > -1)
            {
                text = "SELECT p.PKID AS '序号',c.CompanyName AS '公司',p.ProjectName AS '计划名称',p.Linkman AS '联系人',p.ProjectAddress AS '计划地址' FROM Project AS p INNER JOIN Customer AS c ON p.CustomerID=c.PKID WHERE c.PKID=" + this._nCustomerID + " AND";
            }
            else
            {
                text = "SELECT p.PKID AS '序号',c.CompanyName AS '公司',p.ProjectName AS '计划名称',p.Linkman AS '联系人',p.ProjectAddress AS '计划地址' FROM Project AS p INNER JOIN Customer AS c ON p.CustomerID=c.PKID WHERE";
            }
            if (this.cbCondition.SelectedIndex == 0)
            {
                text = text + " dbo.GetPY(p.ProjectName) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
            }
            if (this.cbCondition.SelectedIndex == 1)
            {
                text = text + " dbo.GetPY(p.Linkman) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
            }
            if (this.cbCondition.SelectedIndex == 2)
            {
                text = text + " dbo.GetPY(p.ProjectAddress) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
            }
            text += " ORDER BY p.PKID DESC";
            Projects projects = new Projects();
            DataTable dataTable = null;
            try
            {
                dataTable = projects.GetProjects(text);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n出现未知错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            projects.Dispose();
            if (dataTable != null)
            {
                this.dgProject.DataSource = null;
                this.dgProject.DataSource = dataTable;
                this.dgProject.AutoResizeColumns();
                dataTable.Dispose();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dgProject.Rows.Count > 0)
            {
                int rowIndex = this.dgProject.CurrentCell.RowIndex;
                FormSelectProject.s_projectInfo = new ProjectData(Convert.ToInt32(this.dgProject.Rows[rowIndex].Cells[0].Value), this.dgProject.Rows[rowIndex].Cells[2].Value.ToString(), this.dgProject.Rows[rowIndex].Cells[3].Value.ToString(), this.dgProject.Rows[rowIndex].Cells[4].Value.ToString());
            }
            else
            {
                MessageBox.Show("表格中没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            base.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void cbCondition_Enter(object sender, EventArgs e)
        {
            base.AcceptButton = this.btnQuery;
        }

        private void txtCondition_Enter(object sender, EventArgs e)
        {
            base.AcceptButton = this.btnQuery;
        }

        private void dgProject_Enter(object sender, EventArgs e)
        {
            base.AcceptButton = this.btnOk;
        }

        public static ProjectData ShowBox(int nCustomerID)
        {
            FormSelectProject formSelectProject = new FormSelectProject(nCustomerID);
            formSelectProject.ShowDialog();
            return FormSelectProject.s_projectInfo;
        }

        private void dgProject_DoubleClick(object sender, EventArgs e)
        {
            this.btnOk_Click(null, null);
        }

        private void dgProject_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.btnOk_Click(null, null);
            }
            if (e.KeyCode == Keys.Up && (this.dgProject.CurrentRow == null || this.dgProject.CurrentRow.Index < 1))
            {
                this.txtCondition.Focus();
            }
        }

        private void txtCondition_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                this.dgProject.Focus();
            }
        }
    }
}
